package com.wildworld.main;

import com.wildworld.core.IMap;
import com.wildworld.core.events.IMapRedrawEventListener;
import com.wildworld.core.worldobjects.WorldObject;
import com.wildworld.coreimpl.WildWorldMapImpl;
import org.apache.commons.collections.Closure;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.collections.functors.ChainedClosure;

import javax.swing.*;
import java.awt.*;
import java.util.Collection;
import java.util.Random;

/**
 * @author gzhebrakov
 *         Date: 18.08.12
 *         Time: 14:31
 */
public class SwingTestWindow extends JFrame implements IMapRedrawEventListener{
    public SwingTestWindow(){
        setBounds(130,40,1600,1000);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }

    @Override
    public void redraw(IMap map) {
        final Graphics g = getGraphics();

        g.setColor(Color.GRAY);
        final int x  = 150;
        final int y = 50;
        g.fillRect(x,y,map.getProperty().getWidth(), map.getProperty().getHeight());

        if (((WildWorldMapImpl)map).getWaterList() != null){
            drawObjects(((WildWorldMapImpl)map).getWaterList(), g, x, y);
        }

        if (((WildWorldMapImpl)map).getHerbList() != null){
            drawObjects(((WildWorldMapImpl)map).getHerbList(), g, x, y);
        }
//
        if (map.getWorldObjects()!= null){
            drawObjects(map.getWorldObjects(), g, x, y);
        }
        g.dispose();
    }

    private void drawObjects(Collection collection, final Graphics g , final int x, final int y){
        CollectionUtils.forAllDo(collection, new Closure() {
            @Override
            public void execute(Object o) {
                WorldObject obj = (WorldObject)o;
                int [] colormas = obj.getColor();
                g.setColor(new Color(colormas[0], colormas[1], colormas[2]));
                g.fillRect(x + obj.getX().intValue(), y + obj.getY().intValue(), obj.getWidth().intValue(), obj.getHeight().intValue());
            }
        });
    }
}
